﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HadronKeygen
{
    public partial class MainForm : Form
    {
        private Random m_random = new Random();

        private long GenerateRandomValue(int p_nOrder)
        {
            long lValue = 0;
            while (p_nOrder-- > 0)
                lValue = lValue * 26 + m_random.Next(26);
            return lValue;
        }

        private String EncodeValue(long p_lValue, int p_nLength)
        {
            String strValue = "";
            while (p_lValue > 0)
            {
                char ch = (char)(p_lValue % 26 + (long) 'A');
                strValue = ch + strValue;
                p_lValue /= 26;
            }

            while (strValue.Length < p_nLength)
                strValue = 'A' + strValue;

            return strValue;
        }

        private String GenerateKey()
        {
            long lValue = GenerateRandomValue(4);
            lValue -= lValue % 11;
            String strSegment1 = EncodeValue(lValue, 4);

            lValue = GenerateRandomValue(4);
            lValue -= lValue % 17;
            String strSegment2 = EncodeValue(lValue, 4);

            lValue = GenerateRandomValue(4);
            lValue -= lValue % 13;
            String strSegment3 = EncodeValue(lValue, 4);

            lValue = GenerateRandomValue(4);
            lValue -= lValue % 29;
            String strSegment4 = EncodeValue(lValue, 4);

            return strSegment1 + "-" + strSegment2 + "-" + strSegment3 + "-" + strSegment4;
        }

        private List<String> GenerateKeys(int p_nCount)
        {
            List<String> listKeys = new List<string>();
            while (p_nCount-- > 0)
                listKeys.Add(GenerateKey());
            return listKeys;
        }

        public MainForm()
        {
            InitializeComponent();
        }

        private void m_btnGenerate_Click(object sender, EventArgs e)
        {
            List<String> listKeys = GenerateKeys(int.Parse(m_txtCount.Text));

            StringBuilder stringBuilder = new StringBuilder();

            foreach (String strKey in listKeys)
            {
                stringBuilder.Append(strKey);
                stringBuilder.Append("\r\n");
            }
            m_txtKeys.Text = stringBuilder.ToString();
        }
    }
}
